<script setup>
  import { computed, ref } from 'vue'
  import { takeDeliveryApi } from '@/apis/task'
  import { onLoad } from '@dcloudio/uni-app'

  // 0. 获取id
  const id = ref('')
  onLoad((params) => {
    id.value = params.id
  })

  // 1. 提货凭证图片
  const receiptPictrues = ref([])
  const cargoPickUpPictureList = computed(() => {
    return receiptPictrues.value.map(({ url }) => {
      return { url }
    })
  })

  // 2. 货品图片
  const goodsPictrues = ref([])
  const cargoPictureList = computed(() => {
    return goodsPictrues.value.map(({ url }) => {
      return { url }
    })
  })

  // 3. 校验
  const enableSubmit = computed(() => {
    return receiptPictrues.value.length > 0 && goodsPictrues.value.length > 0
  })

  // 4. 提交
  const onSubmitForm = async () => {
    // 4.1 准备数据对象
    const formObj = {
      id: id.value,
      cargoPictureList: cargoPictureList.value,
      cargoPickUpPictureList: cargoPickUpPictureList.value,
    }

    // 4.2 调用接口函数
    const { code } = await takeDeliveryApi(formObj)
    if (code !== 200) return uni.utils.toast('提货失败~')

    // 4.3 回到任务列表页
    uni.reLaunch({
      url: '/pages/task/index',
    })
  }
</script>
<template>
  <view class="page-container">
    <view class="receipt-info">
      <uni-file-picker
        v-model="receiptPictrues"
        limit="3"
        title="请拍照上传回单凭证"
        file-extname="jpg,png,gif,webp"
      ></uni-file-picker>
      <uni-file-picker
        v-model="goodsPictrues"
        limit="3"
        title="请拍照上传货品照片"
        file-extname="jpg,png,gif,webp"
      ></uni-file-picker>
    </view>
    <button :disabled="!enableSubmit" class="button" @click="onSubmitForm">
      提交
    </button>
  </view>
</template>

<style lang="scss" scoped>
  .page-container {
    padding: 30rpx;
  }

  .receipt-info {
    min-height: 600rpx;
    background-color: #fff;
    padding: 20rpx 30rpx;
    border-radius: 16rpx;

    ::v-deep .uni-file-picker {
      margin-bottom: 30rpx;
    }
  }

  .button {
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    /* #ifdef APP */
    padding-top: 4rpx;
    /* #endif */
    border-radius: 100rpx;
    margin-top: 60rpx;
    color: #fff;
    font-size: $uni-font-size-big;
    background-color: $uni-primary;

    &[disabled],
    &.disabled {
      color: #fff;
      background-color: #fadcd9;
    }
  }
</style>
